﻿using System;
using System.Diagnostics;
using Caliburn.Micro;

namespace CCWP7.CaliburnMicro
{
    public abstract class ProjectsViewModelBase : Conductor<ProjectViewModel>.Collection.OneActive
    {
        readonly INavigationService _navigationService;

        public ProjectsViewModelBase(INavigationService navigationService)
        {
            Debug.Assert(navigationService != null, "navigationService is null.");

            _navigationService = navigationService;
        }

        protected override void OnInitialize()
        {
            base.OnInitialize();

            RefreshItems();
        }

        public abstract void RefreshItems();

        public override void ActivateItem(ProjectViewModel item)
        {
            Debug.WriteLine("ProjectsViewModelBase.ActivateItem({0})", item);

            if (item == null)
            {
                Debug.WriteLine("server is null");
                return;
            }

            var address = string.Format(Constants.ProjectPageFormat, item.GetHashCode());

            _navigationService.Navigate(new Uri(address, UriKind.Relative));

            base.ActivateItem(item);
        }
    }
}
